package org.example.examfx02.controller;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import org.example.examfx02.entity.Course;
import org.example.examfx02.entity.CourseChoose;
import org.example.examfx02.entity.CourseRequire;
import org.example.examfx02.entity.Teacher;
import org.example.examfx02.HelloApplication;
import org.example.examfx02.tool.StringUtil;
import org.example.examfx02.tool.SystemData;

import java.util.ArrayList;
import java.util.List;

public class ChangeViewController {
    public  static SystemData sys = SystemData.getInstance();
    @FXML
    Label label;
    @FXML
    ChoiceBox tChoiceID;
    @FXML
    TextField textCourseName;
    @FXML
    TextField textNum;
    @FXML
    TextField text;
    @FXML
    Button admitBtn;
    @FXML
    RadioButton radioRequire;
    @FXML
    RadioButton radioChoose;
    @FXML
    ChoiceBox tChoiceBox;
    public void admit(){

        List<Course> courses = sys.getCourses();
        String id = tChoiceID.getValue().toString();
        if(StringUtil.isEmpty(id)){
            HelloApplication.showAlert("警告","ID不能为空！");
            return;
        }
        String name = textCourseName.getText();
        if(StringUtil.isEmpty(name)){
            HelloApplication.showAlert("警告","姓名不能为空！");
            return;
        }
        int chooseNum,typeChoose;
        try{
            chooseNum = Integer.parseInt(textNum.getText());
            typeChoose = Integer.parseInt(text.getText());
            String teacherInfo = tChoiceBox.getValue().toString();
            Teacher t = sys.getUserGroup().getTeacherByInfo(teacherInfo);
            if(t==null){
                HelloApplication.showAlert("警告","请检查教师！");
            }else{
                if(radioRequire.isSelected()){
                    CourseRequire course = new CourseRequire(id,name,chooseNum,t.getWorkId(),typeChoose);
                    sys.updateCourse(course);
                }else{
                    CourseRequire course = new CourseRequire(id,name,chooseNum,t.getWorkId(),typeChoose);
                    sys.updateCourse(course);
                }
            }
        }catch (NumberFormatException e){
            HelloApplication.showAlert("警告","类型或选课人数存在异常！");
        }catch(Exception e){
            HelloApplication.showAlert("警告","请检查教师！");
        }

    }
    public void initialize(){
        ToggleGroup group = new ToggleGroup();
        radioChoose.setToggleGroup(group);
        radioRequire.setToggleGroup(group);
        ArrayList<String> teacherInfo = sys.getUserGroup().getTeachersInfo();
        tChoiceBox.setItems(FXCollections.observableArrayList(teacherInfo));
        ArrayList<String> courseID = sys.getCourseID();
        tChoiceID.setItems(FXCollections.observableArrayList(courseID));
        tChoiceID.setOnAction((event) -> {
//            int selectedIndex = tChoiceID.getSelectionModel().getSelectedIndex();
//            Object selectedItem = tChoiceID.getSelectionModel().getSelectedItem();
            Course course = sys.getCourseById(tChoiceID.getValue().toString());
            course.show();
            if(course!=null){
                if(course.getType()==0){
                    radioRequire.setSelected(true);
                    text.setText(String.valueOf(((CourseRequire)course).getCredit()));
                    changeRadioLabel();
                }else if(course.getType()==1){
                    radioChoose.setSelected(true);
                    text.setText(String.valueOf(((CourseChoose)course).getMaxNum()));
                    changeRadioLabel();
                }
                textCourseName.setText(course.getName());
                textNum.setText(String.valueOf(course.getChooseNum()));
                Teacher t = sys.getUserGroup().getTeacherByID(course.getTeacherId());
                if(t!=null){
                    String teaInfo = t.toString();
                    tChoiceBox.setValue(teaInfo);
                }
            }

        });
    }
    public void changeRadioLabel(){
        if(radioRequire.isSelected()){
            label.setText("学分：");
        }else{
            label.setText("限选人数：");
        }
    }

}
